package depot;

import java.util.ArrayList;


public class Customerlist {
	
private ArrayList<Customer> customerList;

	/**
	 * create an empty ArrayList
	 */

	public Customerlist() {
		
		customerList = new ArrayList<Customer> ();
	}
	
	
	 /**
	  * find whether the id of parcel exist in CustomerList
	    if the id does not exist, add the id  into CustomerList,return true
	     else return false
	  * @param c customer object is the parameter to be added
	  * @return boolean 
	  */
	
	public boolean addCustomer(Customer c){
		String ID = c.getParcelID();
		if(this.findCustomerByID(ID) == false){
			customerList.add(c);
			return true;
		}
		return false;
	}
	
	/**
	 * return the details of customer object
	 * @return customerList  the whole object exists in customerList
	 */
	public ArrayList<Customer> getCustomerList(){
		 
		return customerList;
	}
	/**
	 * 
	 * @param ID
	 * @return
	 */
	private boolean findCustomerByID(String ID)
    {
    	for (Customer c : customerList)
    	{
    		if (c.getParcelID().equals(ID))
    		{
    			return true;
    		}
    	}
    	return false;
    }
	/**
	 * returns the customer object with a specified sequence No
	   demonstrates searching through the array  and stopping by returning when a match is found
	   shows details of a specified ID
	 * @param ID  the id of parcel 
	 * @return c  show the details of customer or null
	 */
	
	    public Customer findCustomer(int seqNo)
	    {
	    	for (Customer c : customerList)
	    	{
	    		if (c.getSeqNo() == seqNo)
	    		{
	    			return c;
	    		}
	    	}
	    	return null;
	    }
	    
	    /**
	     * remove the record of parcel from customerList depend on a specified parcel ID
	     * @param ID the id of parcel
	     * @return boolean  if the customer has been removed, return true, else false
	     */
	    public boolean removeCustomer(String ID)
	    {
	    	for (Customer c : customerList)
	    	{
	    		if (c.getParcelID().equals(ID))
	    		{
	    			return customerList.remove(c) == true;	
	    		}
	    	}
	    	return false;
	    }
		
	    /**
	     * get the size of customerList
	     * @return size the size of customerList
	     */
	    public int getCustomerSize(){
	    	
	    	return customerList.size();
	    	
	    }
	    
	    /**
		 * Processes line, extracts data, creates Customer object
		 * and adds to list
		 * Checks for non-numeric ID and missing items
		 * Will still crash if name entered without a space
		 * @param line the line to be processed
	     * @throws NameValueException,IDFormatException,ArrayStoreException
		 */
		public void processLine(String line) {
			
			try{
				
				String parts [] = line.split(",");
			    
				//automated generated the sequence number
					int seqNo = 1;
					if(customerList.size() != 0 ){
						int lastindex = customerList.size()-1;
				    	Customer c = customerList.get(lastindex);
				    	 seqNo = c.getSeqNo()+1;
					}
			    
				Name customername = new Name(parts[0]);
				
				String parcelID = parts[1];
				
				
				//create Student object and add to the list
				Customer c = new Customer(seqNo,customername, parcelID );
				
			
				this.addCustomer(c);
		
			}
			catch(IDFormatException e){
				System.out.print(e.getMessage());
			}
			
			catch(ArrayStoreException e){
				System.out.print("The parts"+e.getMessage());
			} 
			
			catch (NameValueException e) {
				
				System.out.print(e.getMessage());
			}
		}
		
				
}
